
#ifndef _PROCESSROUTER_PROCESSHUB_H_
#define _PROCESSROUTER_PROCESSHUB_H_

#include <ev.h>
#include <list>
#include <map>
#include <string>
#include "iothread.h"
#include "zkthread.h"
#include "config.h"
#include "message.h"
#include "channel.h"
#include "webkit_message_handle.h"
#include "manager_message_handle.h"
#include "zmq_message_handle.h"
#include "manager_info.h"
#include "packagefilter.h"
#ifndef Q_MOC_RUN 
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#endif

#define SYSTEM_MEMORY_LIMIT_PERCENT 0

namespace ProcessRouter {

class ProcessHub {
    public:
        ~ProcessHub();

        static ProcessHub *instance(std::string path = std::string(CONFIGFILE));
        static ManagerInfo *info();
        std::string serializePageInfo();

        void bind();
        void close();
        void sendKill(FifoChannel* fc);

        int createProcess(bool isStatic = false);

        bool clearChild(Rooftop::EventBase &e);
        bool childUpdateProxy(Rooftop::EventBase &e);

        FifoChannel* getChannel(int aimpid);

    private:
        ProcessHub();

        void subProcessMain();
        void clearFifoChannel(int cpid);

        bool onRecvPackage(Rooftop::EventBase &e);
        void taskLoop(Rooftop::Task<ProcessHub> *);
        void updateProxy();

        std::map<int, FifoChannel*> m_fifos;
        std::list<int> m_staticProcess;
        std::list<int> m_dynamicProcess;
        static ProcessHub *m_instance;
        static ManagerInfo *m_managerInfo;
        IOThread *m_iothread;
        ZKThread *m_zkthread;

        Rooftop::Task<ProcessHub> m_task;
        PackageFilter m_filter;
        bool m_bindEnd;
        std::list<FifoChannel*> m_needDeleteChannel;

        // for sub process
        FifoChannel *m_channel;
        ZmqChannel *m_zmqchannel;
        bool m_isStatic;

        int m_currentProxyPos;
        std::string m_proxy;
};

}

#endif

